
package com.microweb.toastmaster.dao.hibernate;

import java.util.List;
import com.microweb.toastmaster.model.Actor;
import com.microweb.toastmaster.dao.ActorDao;
import java.util.ArrayList;

import org.springframework.orm.ObjectRetrievalFailureException;

public class ActorDaoHibernate extends BaseDaoHibernate implements ActorDao {

    /**
     * @see com.microweb.toastmaster.dao.ActorDao#getActors(com.microweb.toastmaster.model.Actor)
     */
    public List<Object> getActors(final Actor actor) {
        return getHibernateTemplate().find("from Actor");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.
        if (actor == null) {
            return getHibernateTemplate().find("from Actor");
        } else {
            // filter on properties set in the actor
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(actor).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(Actor.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }*/
    }

    /**
     * @see com.microweb.toastmaster.dao.ActorDao#getActor(Long id)
     */
    public Actor getActor(final Long id) {
        Actor actor = (Actor) getHibernateTemplate().get(Actor.class, id);
        if (actor == null) {
            log.warn("uh oh, actor with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(Actor.class, id);
        }

        return actor;
    }

    /**
     * @see com.microweb.toastmaster.dao.ActorDao#saveActor(Actor actor)
     */    
    public void saveActor(final Actor actor) {
        getHibernateTemplate().saveOrUpdate(actor);
    }

    /**
     * @see com.microweb.toastmaster.dao.ActorDao#removeActor(Long id)
     */
    public void removeActor(final Long id) {
        getHibernateTemplate().delete(getActor(id));
    }
    
    
    public List<Object> getActors(final String username, final String rolename) {
	String queryStr = "";
	
	String queryUser = "";
	if (!isNullOrSpace(username)) {
	    queryUser = "Actor.user.username='" + username + "'";
	}
	
	String queryRole = "";
	if(!isNullOrSpace(rolename)) {
	    queryRole = "";
	}
	
	if(isNullOrSpace(queryUser)) {
	    if(isNullOrSpace(queryRole)) {
		return new ArrayList<Object>();
	    } else {
		queryStr = "from Actor where Actor.TMRole.name='" + rolename + "'";
	    }
	} else {
	    if(isNullOrSpace(queryRole)) {
		queryStr = "from Actor where Actor.user.username='" + username + "'";
	    } else {
		queryStr = "from Actor where Actor.TMRole.name='" + rolename + "'"
			+ " and Actor.user.username='" + username + "'";
	    }
	}
	
	return getHibernateTemplate().find(queryStr);
    }
    
    private boolean isNullOrSpace(String str) {
	if (str == null || str.trim().length() == 0)
	    return true;
	
	return false;
    }
}
